var $ = require('../../lib/jquery');
var util = require('./common/util');
var nav = require('./common/nav');
var template = require('../tpl/template');
var _header = require('./common/header');
var jyqy_tab = require('./common/jy_common');
var _nav = new nav();
var header = new _header();
var change_family_doctor;
var docdata;
change_family_doctor = {
  type: 1,
  page: 1,
  maxpage: null,
  init: function () {
    if (location.href.indexOf('jlda') > -1) {
      $('.js_jy_select').hide();
      $('.js_doclist_wrap').html('正在建设中，敬请期待').css({
        fontSize: '20px'
      })
    } else {
      this.fetch();
      this.bindevent();

    }


  },
  getParams: function (flag) {
    this.type = $('.js_jy_select').find('.on').data().type;
    if(this.type==1){
      $('.js_input').attr('placeholder', '请输入机构名称')
    }else{
      $('.js_input').attr('placeholder', '请输入医生姓名')
    }
    var search = !!flag && $('.js_input').val() || '';
    return {
      type: this.type,
      search: search || '',
      page: this.page,
      pageSize: 10
    }
  },
  fetch: function (flag) {
    var _this = this;
    util.fetch('api/medical/dock/queryHomeMedical', this.getParams(flag), {
      timeout: 1000
    }).then(function (res) {
      _this.maxpage = res.data.totalPage;
      _this.render(res.data);
    }).fail(function () {
      console.log('请求错误异常流')
    })

  },
  render: function (data) { // dom渲染； 0为家医 1为机构
    var d = data.list || [];
    var tpl = this.type == 1 ? template('jgjy', {
      data: d
    }) : template('doctor_list', {
      data: d
    });
    this.type == 0 ? docdata = data.list:'';
    $('.js_doclist_wrap').html(tpl);
    if (this.maxpage <= 1) {
      $('.js_btn_wrap').addClass('hide');
    } else {
      $('.js_btn_wrap').removeClass('hide');
      $('.js_num_wrap').html(this.page + '/' + this.maxpage);
      if (this.page == 1) {
        $('.js_changepage_reduce').removeClass('blue');
      } else {
        $('.js_changepage_reduce').addClass('blue');
      }
      if (this.page == this.maxpage) {
        $('.js_changepage_add').removeClass('blue');
      } else {
        $('.js_changepage_add').addClass('blue');
      }
    }
  },
  getdetail: function (orgcode, doctorName, orgname, falg) { // flag 是否为签约的标识位
    util.fetch('api/medical/dock/queryArchives').then(function (res) {
      if (falg) {
        if (res.data.code == '109') {
          $('.js_qy_layer').removeClass('hide');
          $('.js_qy_wrap').removeClass('hide');
          $('.js_qy_wrap .js_qy_name').html(doctorName);
          $('.js_qy_wrap .js_qy_jg').html(orgname);
         
        }else{
          
        }
      }

    }).fail(function (res) {
      console.log(res);
    })
  },
  bindevent: function (data) { // 翻页功能
    var _this = this;
    $('.js_qy_wrap .x').on('click', function () {
      $('.js_qy_layer').addClass('hide');
      $('.js_qy_wrap').addClass('hide');
    })
    $('.js_check').on('click',function(){
      if($(this).hasClass('check')){
        $(this).removeClass('check')
      }else{
        $(this).addClass('check')
      }
    })
    $('.js_jy_select').on('click', 'p', function () {
      if ($(this).data().type != undefined) {
        $(this).addClass('on')
        $(this).siblings().removeClass('on');
        _this.page = 1;
        _this.fetch();
      }
    });
    $('.js_btn_wrap').on('click', '.js_changepage_add', function (res) {
      if (_this.page < _this.maxpage) {
        _this.page++;
        _this.fetch();
      }
    });
    $('.js_btn_wrap ').on('click', '.js_changepage_reduce', function () {
      if (_this.page > 1) {
        _this.page--;
        _this.fetch();
      }
    });
    $('body').on('click', '.js_toqy', function () { // 去签约到签约页面
      var orgcode = $(this).data().orgcode;
      var doctorName = $(this).data().name;
      var orgname = $(this).data.orgname;
      $('.js_qy_layer').removeClass('hide');
      $('.js_qy_wrap').removeClass('hide');
      $('.js_qy_wrap .js_qy_name').html(doctorName);
      $('.js_qy_wrap .js_qy_jg').html(orgname);
       sessionStorage.setItem('doctorName', doctorName);
       sessionStorage.setItem('orgcode', orgcode);
      _this.getdetail(orgcode, doctorName, orgname, true);
      return false;

    });
    $('body').on('click', '.js_to_xq', function () {
      var index = $(this).index();
      sessionStorage.setItem('docxq', JSON.stringify(docdata[index]))
      location.href = './jyxq.html';
    })
    $('.js_btn').on('click', function () {
        if ($('.js_check').hasClass('check')) {
          util.fetch('api/medical/dock/signingHomeMedical', {
            doctorName: sessionStorage.getItem('doctorName'),
            orgcode:sessionStorage.getItem('orgcode'),
          }).then(function (res) {
            alert(res.msg);
            location.href = './jyqy.html'
          }).fail(function(res){
             $('.js_qy_layer').addClass('hide');
             $('.js_qy_wrap').addClass('hide');
            alert(res.msg)
          })
        } else {
          alert('请先同意签约')
        }
      })
    $('.js_doclist_wrap').on('click', '.js_jgjs', function () { // 查看机构介绍
      var orgcode = $(this).data().id;
      location.href = './jgjs.html?orgcode=' + orgcode;
      return false;
    })
    $('.js_index').on('click', function () {
      location.href = "./index.html";
    })
    $('.js_input_wrap').on('click', function () {
      _this.fetch(true);
    })
  },

}
change_family_doctor.init();
jyqy_tab.bindevent();